package cn.elegent.lockdemo.controller;
import cn.elegent.lock.ElegentLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 分布式锁的测试工程(代码方式)
 */
@RestController
@Slf4j
public class LockCodeController {

    @Autowired
    private ElegentLock eLegentLock;

    /**
     * 代码方式加锁与释放锁
     * @param name
     * @return
     */
    @GetMapping("/lock/{name}")
    public boolean lock( @PathVariable String name) {
        boolean b = eLegentLock.lock(name,60,false);
        //todo: 业务逻辑
        System.out.println("业务逻辑"+b);
        //eLegentLock.unLock(name);
        return b;
    }


    /**
     * 释放锁
     * @param name
     * @return
     */
    @GetMapping("/unlock/{name}")
    public boolean unlock(@PathVariable String name) {
        boolean b = eLegentLock.unLock(name);
        System.out.println("释放锁"+b);
        return b;
    }

}
